﻿using System;
using System.IO;
using System.Runtime.Serialization.Json;
using System.Text;
using System.Text.RegularExpressions;
using System.Xml;

namespace Matrix.Common.Utility
{
    public static class JsonSerializerHelper
    {
        //public static T Deserialize<T>(string jsonString)
        //{
        //    const string pattern = @"\d{4}-\d{2}-\d{2}\s\d{2}:\d{2}:\d{2}";
        //    var matchEvaluator = new MatchEvaluator(ConvertDateStringToJsonDate);
        //    var reg = new Regex(pattern);
        //    jsonString = reg.Replace(jsonString, matchEvaluator);

        //    var ser = new DataContractJsonSerializer(typeof(T));
        //    byte[] buffer = Encoding.UTF8.GetBytes(jsonString);
        //    var stream = new MemoryStream(buffer) { Position = 0 };
        //    return (T)ser.ReadObject(stream);
        //}

        //public static string Serialize<T>(T t)
        //{
        //    //var ser = new DataContractJsonSerializer(typeof(T)); 
        //    //var stream = new MemoryStream();
        //    //ser.WriteObject(stream, t);
        //    //return Encoding.UTF8.GetString(stream.GetBuffer());

        //    string result = String.Empty;
        //    try
        //    {
        //        var serializer = new DataContractJsonSerializer(typeof(T));
        //        using (var ms = new MemoryStream())
        //        {
        //            serializer.WriteObject(ms, t);
        //            result = Encoding.UTF8.GetString(ms.ToArray());

        //            const string pattern = @"\\/Date\((\d+)\+\d+\)\\/";
        //            var matchEvaluator = new MatchEvaluator(ConvertJsonDateToDateString);
        //            var reg = new Regex(pattern);
        //            result = reg.Replace(result, matchEvaluator);
        //        }
        //    }
        //    catch (Exception ex)
        //    {
        //        throw ex;
        //    }
        //    return result;
        //}

        /// <summary>
        /// 将Json序列化的时间由/Date(1304931520336+0800)/转为字符串
        /// </summary>
        private static string ConvertJsonDateToDateString(Match m)
        {
            string result = string.Empty;
            DateTime dt = new DateTime(1970, 1, 1);
            dt = dt.AddMilliseconds(long.Parse(m.Groups[1].Value));
            dt = dt.ToLocalTime();
            result = dt.ToString("yyyy-MM-dd HH:mm:ss");
            return result;
        }
        
        /// <summary>
        /// 将时间字符串转为Json时间
        /// </summary>
        private static string ConvertDateStringToJsonDate(Match m)
        {
            string result = string.Empty;
            DateTime dt = DateTime.Parse(m.Groups[0].Value);
            dt = dt.ToUniversalTime();
            TimeSpan ts = dt - DateTime.Parse("1970-01-01");
            result = string.Format("\\/Date({0}+0800)\\/", ts.TotalMilliseconds);
            return result;
        }

        public static string ConvertXmlToJson(string xmlContent)
        {
            var doc = new XmlDocument();
            doc.LoadXml(xmlContent);
            string json = Newtonsoft.Json.JsonConvert.SerializeXmlNode(doc);

            return json;
        }
    }
}
